<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${ctx}/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctx}/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">借阅人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="readerName" lay-verify="required"
                                       lay-reqtext="借阅人不能为空" placeholder="请输入读者姓名"
                                       value="${(lendDTO.readerName)!}" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">书名</label>
                            <div class="layui-input-inline">

                                <input type="text" name="bookName" lay-verify="required" lay-reqtext="书名不能为空"
                                       placeholder="请输入书名" value="${(lendDTO.bookName)!}" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script src="${ctx}/lib/layui/layui.js" charset="utf-8"></script>
<script>
    var ctx = "${ctx}";
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: ctx + '/lend/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'readerName', width: 100, title: '借阅人'},
                {field: 'bookName', width: 100, title: '书名', sort: true},
                {field: 'lendDate', width: 120, title: '借书日期'},
                {field: 'dueDate', width: 120, title: '还书日期'},
                {field: 'updateTime', width: 150, title: '更新时间', sort: true},
                {field: 'bookStatus', width: 120, title: '图书状态', sort: true},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        $(".layui-btn-primary").click(function () {
            //执行搜索重载
            tableIns.reload(/*'currentTableId',*/ {
                page: {
                    curr: 1
                }
                , where: {
                    // 通过文本框/下拉框的值，设置传递的参数
                    readerName: $("[name='readerName']").val() // 客户名称
                    , bookName: $("[name='bookName']").val() // 创建人
                }
            }/*, 'data'*/);
            /*
                        return false;*/
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                openAddPage();

            } else if (obj.event === 'delete') {  // 监听删除操作
                deletePage();
            }
        });

        /*打开添加/修改营销机会数据的窗口
                 如果营销机会ID为空，则为添加操作
                 如果营销机会ID不为空，则为修改操作*/

        function openAddPage(lendId) {
            // 弹出层的标题
            var title = "<h3>书籍借阅 - 添加借阅记录</h3>";
            var url = ctx + "/lend/toAddOrUpdatePage";

            // 判断借阅ID是否为空
            if (lendId != null && lendId !== '') {
                // 更新操作
                title = "<h3>书籍借阅 - 修改借阅记录</h3>";
                // 请求地址传递营销机会的ID
                url = url + '?lendId=' + lendId;
            }
            layui.layer.open({
                title: title,
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['500px', '620px'],
                content: url,
            });

        }

        function deletePage() {
            // 获取数据表格选中的行数据   table.checkStatus('数据表格的ID属性值');
            var checkStatus = table.checkStatus("currentTableId");
            console.log(checkStatus);

            // 获取所有被选中的记录对应的数据
            var lendData = checkStatus.data;

            // 判断用户是否选择的记录 (选中行的数量大于0)
            if (lendData.length < 1) {
                layer.msg("请选择要删除的记录！", {icon: 5});
                return;
            }

            // 询问用户是否确认删除
            layer.confirm('您确定要删除选中的记录吗？', {icon: 3, title: '管理'}, function (index) {
                // 关闭确认框
                layer.close(index);
                // 传递的参数是数组   ids=1&ids=2&ids=3
                var ids = "ids=";
                // 循环选中的行记录的数据
                for (var i = 0; i < lendData.length; i++) {
                    if (i < lendData.length - 1) {
                        ids = ids + lendData[i].id + "&ids="
                    } else {
                        ids = ids + lendData[i].id;
                    }
                }
                // console.log(ids);

                // 发送ajax请求，执行删除营销机会
                $.ajax({
                    type: "post",
                    url: ctx + "/lend/delete?" + ids,
                    // data: ids, // 传递的参数是数组 ids=1&ids=2&ids=3
                    success: function (result) {
                        // 判断删除结果
                        if (result.code == 200) {
                            // 提示成功
                            layer.msg("删除成功！", {icon: 6});
                            // 刷新表格
                            tableIns.reload();
                        } else {
                            // 提示失败
                            layer.msg(result.msg, {icon: 5});
                        }
                    }
                });
            });
        }

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var id = obj.data.id;
                console.log(id);
                openAddPage(id);
                return false;
            } else if (obj.event === 'delete') {
                // 弹出确认框，询问用户是否确认删除
                layer.confirm('确定要删除该记录吗？', {icon: 3, title: "管理"}, function (index) {
                    // 关闭确认框
                    layer.close(index);
                    console.log(data.id);
                    // 发送ajax请求，删除记录
                    $.ajax({
                        type: "post",
                        url: ctx + "/lend/delete",
                        data: {
                            ids: data.id
                        },
                        success: function (result) {
                            // 判断删除结果
                            if (result.code == 200) {
                                // 提示成功
                                layer.msg("删除成功！", {icon: 6});
                                // 刷新表格
                                tableIns.reload();
                            } else {
                                // 提示失败
                                layer.msg(result.msg, {icon: 5});
                            }
                        }
                    });
                });
            }

        })


    });

</script>

</body>
</html>